<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String ctx = request.getContextPath();%>
<title>paper list</title>
</head>

<script>
	$(document).ready(function(){
		$.ajax(
			{
				dataType: "json",
				type: "GET",
				url: "<%=ctx%>/exam?act=getAll",	
				data: {},	
				success: function(data, status) {
					let select = $("#exam")
					select.empty();
					let idx = 1;
					$.each(data, function(i, e) {
						select.append("<option value='" + e.id + "'>"+ e.name +"</option>");
						idx += 1;
					})
				},
				error: function(req, status, msg) {
					alert(msg)
				}
			}
		)
	});

	function createPaper() {
		let paperName = $("#papername").val();
		if(paperName.length === 0) {
			alert('请输入试卷名称');
		} else {
			$.ajax(
				{ type:"POST",
				  url: "<%=ctx%>/paper?act=addPaper",
				  data: {
						"papername" :paperName
					},	
				  success: function(data, status) {
					  alert(data);
					  AjaxLoad('<%=ctx%>/control?act=paperOpera');
				  },
				  error: function(req, status, msg) {
					  alert(msg)
				  }
				}
			)
		}
	}
	function deletePaper(PaperId) {
	}

	function exportPaper(PaperId) {
		let examName = $("#exam").find("option:selected").text();
		$.ajax(
				{
					type:"GET",
					url: '<%=ctx%>/paper?act=exportPaper',
					dataType: "json",
					data: {
						"paperid": PaperId,
						"examname":examName,
					},
					success: function(data, status) {
						alert(data);
					},
					error: function(req, status, msg) {
						alert(msg);
					}
				}
		)
	}
	
	
	function addToExam(id) {
		
		let examName = $("#exam").find("option:selected").text();
		if(confirm("添加到 "+examName+"?") !== true)
			return;
		
		$.ajax(
			{
				type:"GET",
				url: '<%=ctx%>/exam?act=addPaperToExam',
				dataType: "json",
				data: {
					"paperid": id,
					"examname":examName,
				},
				success: function(data, status) {
					alert(data);
				},
			  	error: function(req, status, msg) {
				 	alert(msg);
			  	}
			}
		)
	}

</script>


<body>
	<div id="list" class="col-md-10 col-xs-12">
		<h3>试卷列表</h3>
		<hr>
		<div class="fiter col-md-10 col-xs-12">
			<form action="" class="form-horizontal form-inline">
				<input class="form-control" type="text" id="papername" placeholder="试卷名称">
				<button type="button" class="btn btn-success" onclick="createPaper()">创建试卷</button>
			</form>
		</div>
		
  		<div class="fiter col-md-10 col-xs-12">
				<h5>选择一个考试</h5>
				<form action="" class="form-horizontal form-inline">
					<select class="form-control" id="exam"/>
				</form>
		</div>
  
<table class="table table-hover">
	<thead> <tr>
			<th>序号</th>
			<th>试卷</th>
			<th>操作</th>
	</tr></thead>
	
	<tbody id="paperbody"> 
	<c:forEach items="${PaperList}" var="Paper" varStatus="s">
		<tr>
				<td>${s.index + 1}</td>
				<td>${Paper.name}</td>
				<td>
<button type="button" class="btn btn-success" onclick="addToExam('${Paper.id}')">
	添加到考试</button>
<button type="button" class="btn btn-warning" onclick="AjaxLoad('<%=ctx%>/paper?act=editPaper&paperid=' + '${Paper.id}')">
		编辑</button>
<button type="button" class="btn btn-primary" onclick="">
						重命名</button>
<button type="button" class="btn btn-info" onclick="exportPaper('${Paper.id}')">
						导出</button>
<button type="button" class="btn btn-danger" onclick="deletePaper('${Paper.id}')">
						删除</button>
				</td>
		</tr>
	</c:forEach>
	</tbody>
</table>
	</div>
</body>
</html>